#include "gridScreen.h"
#include <iostream>
using namespace std;

//mapScreen------------------------------------------------
//access--------------------------
int gridScreen::getLineWidth() {
    return lineWidth;
}
int gridScreen::getLineHeight() {
    return lineHeight;
}
//view manips---------------------
void gridScreen::draw() {
    //get cairo instance
    cairo_t* cr = cairo_create(getCairoSurface());
    cairo_set_source_rgb(cr, 0, 0, 0);

    //setup the background
    Uint32 BlueColor = SDL_MapRGBA(SDL_GetVideoSurface()->format, 0, 0, 0, 1);
    SDL_FillRect(drawSurface, &drawPosition, BlueColor);

    //draw lines
    //vert
    for (int i = 0; i <= screenSize.w; i += lineWidth) {
        if (highlightLine > 0 && i % (highlightLine * lineHeight) == 0) {
            cairo_set_dash(cr, highDash,highDashSize, 0);
            cairo_set_line_width(cr, 4);
            cairo_set_source_rgb(cr, highR, highG, highB);
        } else {
            cairo_set_dash(cr, lowDash, lowDashSize, 0);
            cairo_set_source_rgb(cr, lowR, lowG, lowB);
            cairo_set_line_width(cr, 2);
        }
        cairo_move_to(cr, i, 0);
        cairo_line_to(cr, i, screenSize.h);
        cairo_stroke(cr);
    }
    for (int i = 0; i <= screenSize.h; i += lineWidth) {
        if (highlightLine > 0 && i % (highlightLine * lineHeight) == 0) {
            cairo_set_dash(cr, highDash,highDashSize, 0);
            cairo_set_line_width(cr, 4);
            cairo_set_source_rgb(cr, highR, highG, highB);
        } else {
            cairo_set_dash(cr, lowDash, lowDashSize, 0);
            cairo_set_line_width(cr, 2);
            cairo_set_source_rgb(cr, lowR, lowG, lowB);
        }
        cairo_move_to(cr, 0, i);
        cairo_line_to(cr, screenSize.w, i);
        cairo_stroke(cr);
    }
    //refresh images
    list<image*>::iterator imItr;
    for (imItr = images.begin(); imItr != images.end(); imItr++) {
        (*imItr)->draw();
    }

    //draw children
    map<screen*, int*>::iterator scritr;
    for(scritr = children.begin(); scritr != children.end(); scritr++){
        (scritr->first)->draw();
    }

    //blit surface
    SDL_BlitSurface(drawSurface, &drawPosition, window, &windowPosition);
    cairo_destroy(cr);
}


//lines---------------------------
void gridScreen::setScale(float scale) {

}
void gridScreen::setLines(int width, int height) {
    lineWidth = width;
    lineHeight = height;
}
void gridScreen::drawLines(bool lines) {
    drawlines = lines;
}
void gridScreen::setHighLinesDash(double* spacing, int arrSize){
    highDash = spacing;
    highDashSize = arrSize;
}
void gridScreen::setLineDash(double* spacing, int arrSize){
    lowDash = spacing;
    lowDashSize = arrSize;

}
void gridScreen::setHighlightLineSpacing(int spaceing){
    highlightLine = spaceing;
}
void gridScreen::setHighlightLineRGB(float R, float G, float B){
    highR = R;
    highG = G;
    highB = B;
}
void gridScreen::setLineRGB(float R, float G, float B){
    lowR = R;
    lowG = G;
    lowB = B;
}
//mapScreen================================================